// pages/authorize/index.js
var app = getApp();
var md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_collect:0,
    is_zan:0,
    talk_id:0,
    is_close: 'none',
    talk_zan_count:0,
    talk_pinglun_count:0,
    talk_zhuanfa_count:0,
    pingLun_type: false,
    qiang: false,
    qiang_text: 6,
    display: 'none',
    open: 'chai',
    open_state: true,
    qiang_money: 0,
    display_prompt: 'none',
    red_count: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    var intervalId = setInterval(function () {
      var c = that.data.qiang_text

      if (c > 1) {
        c = c - 1;
        that.setData({
          qiang_text: c,
          qiang: false
        })
      } else {
        that.setData({
          qiang_text: '抢',
          qiang: true
        })
      }
    }, 1000)

    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    var service = 'Talk.GetInfo'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e.id
    var sign = md5.md5(open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id
      },
      success: function (res) {
        wx.hideToast()
        that.pingLun(res.data.data.info.talk_id)
        that.setData({
          talk: res.data.data.info,
          talk_id: res.data.data.info.talk_id,
          is_collect: res.data.data.info.is_collect,
          is_zan: res.data.data.info.is_zan,
          talk_zan_count: res.data.data.info.talk_zan_count,
          talk_pinglun_count: res.data.data.info.talk_pinglun_count,
          talk_zhuanfa_count: res.data.data.info.talk_zhuanfa_count,
          red_id: res.data.data.info.talk_red_id
        });
        if(res.data.data.info.talk_red_id > 0){
          that.getRedRecord(res.data.data.info.talk_red_id)
        }
      },
      fail: function (res) {
        wx.hideToast()
      }
    })
  },
  pingLun: function (e){
    var that = this;
    var service = 'Talk.PingLunList'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e
    var sign = md5.md5(open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          pingLun: res.data.data.info
        });
      }
    })
  },
  getRedRecord:function(e){
    var that = this;
    var service = 'Shop.GetRedRecord'
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var red_id = e
    var page = 1
    var page_count = 10
    var sign = md5.md5(openid + page + page_count + red_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        red_id: red_id,
        page: page,
        page_count: page_count
      },
      success: function (res) {
        console.log(res)
        that.setData({
          red: res.data.data.info,
          red_count: res.data.data.info.length
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return {
      title: '123456',
    }
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  rejectLogin: function (e) {
  },
  bindGetUserInfo: function (e) {
  },
  login: function () {
  },
  registerUser: function () {
  },
  imgYu: function (e) {//图片预览
    var src = e.currentTarget.dataset.src;//获取data-src
    var imgList = e.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  guanZhu:function(e){
    if (e.currentTarget.dataset.type == 0) {
      var collect_val = 1
    } else {
      var collect_val = 0
    }
    var that = this;
    var service = 'Talk.Collect'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e.currentTarget.dataset.id
    var collect = collect_val
    var sign = md5.md5(collect + open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id,
        collect: collect
      },
      success: function (res) {
        if (res.data.data.code == 0) {
          that.setData({
            is_collect: collect_val
          });
        }
      }
    })
  },
  znAn:function(e){
    var that = this;
    if (e.currentTarget.dataset.type == 0) {
      var collect_val = 1
      var talk_zan_count = that.data.talk_zan_count * 1 + 1
    } else {
      var collect_val = 0
      var talk_zan_count = that.data.talk_zan_count * 1 - 1
    }
    var service = 'Talk.Zan'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e.currentTarget.dataset.id
    var collect = collect_val
    var sign = md5.md5(collect + open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id,
        collect: collect
      },
      success: function (res) {
        if (res.data.data.code == 0) {
          that.setData({
            is_zan: collect_val,
            talk_zan_count: talk_zan_count
          });
        }
      }
    })
  },
  pingLunBut:function(e){
    var that = this;
    if (e.currentTarget.dataset.v) {
      var pingLun_type = false
    } else {
      var pingLun_type = true
    }
    that.setData({
      pingLun_type: pingLun_type
    });
  },
  formSubmit:function(e){
    wx.showToast({
      title: '评论中',
      icon: 'loading'
    })
    var that = this;
    var service = 'Talk.PingLun'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e.currentTarget.dataset.id
    var content = e.detail.value.value
    var sign = md5.md5(content + open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id,
        content: content
      },
      success: function (res) {
        wx.hideToast()
        if (res.data.data.code == 0) {
          var talk_pinglun_count = that.data.talk_pinglun_count*1 + 1
          that.pingLun(e.currentTarget.dataset.id)
          that.setData({
            pingLun_type: false,
            talk_pinglun_count: talk_pinglun_count
          });
        } else {
          wx.showToast({
            title: '评论失败',
          })
        }
      },
      fail: function (res) {
        wx.hideToast()
      }
    })
  },
  
  open_red: function (e) {
    var that = this;
    that.setData({ open: "rotate" })
    setTimeout(function () {
      that.setData({ 
        open: "chai", 
        open_state: false,
        is_close: 'hide'
        })
    }, 1200)
  },
  click_close_red: function (e) {
    var that = this;
    that.setData({ open: "chai", open_state: true, display: 'none' })
  },


  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    var that = this
    var share = {
      title: that.data.talk.talk_name + '邀你加入生活圈！',
      path: '',
      imageUrl: that.data.talk.avatar,
      success: function (res) {
        　　　　　　// 转发成功之后的回调
      if (res.errMsg == 'shareAppMessage:ok') {
          var openid = wx.getStorageSync('openid');
          var user_id = wx.getStorageSync('user_id');
          var data_id = that.data.talk_id
          var zhuanfa_type = 'talk'
          var service = 'System.ZhuanFa';
          var sign = md5.md5(data_id + openid + service + user_id + zhuanfa_type);
          wx.request({
            url: app.globalData.apiUrl + '?service=System.ZhuanFa',
            header: {
              'Content-Type': 'json'
            },
            data: {
              sign: sign,
              open_id: openid,
              user_id: user_id,
              data_id: data_id,
              zhuanfa_type: zhuanfa_type
            },
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '转发成功'
              })
            }
          })
        　　　　　　}
      　　　　}
    };
    return share
  },
  prompt_none:function(){
    console.log('隐藏')
    this.setData({ display_prompt: 'none' })
  },
  pushShop: function (e) {
    console.log(e.currentTarget.dataset['id']);
    var index = e.currentTarget.dataset['id'];
    var user_phone = wx.getStorageSync('userPhone');
    if (user_phone) {
      wx.navigateTo({
        url: "/pages/shop-detail/index?id=" + index
      })
    } else {
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
    that.getlod();
  },
  share_type: function () {

  },
  getlod: function () {
    var that = this;
    var service = 'User.CheckLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }
      }
    })
  },
})